//
//  ChinaArea.h
//  地址选择器
//
//  Created by zhuming on 16/2/15.
//  Copyright © 2016年 zhuming. All rights reserved.
//

#import "LBCityDataManager.h"
#import <MJExtension/MJExtension.h>

static LBCityDataManager * shareCityManager = nil;

@interface LBCityDataManager ()

@property (nonatomic,strong)NSMutableArray * dataArray;

@property (nonatomic,strong)NSMutableArray <LBCityMsgModel *> * cityModelArray;   // 模型数据
@end

@implementation LBCityDataManager

+ (void)load {
    
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        
        shareCityManager = [[LBCityDataManager alloc] init];
    });
}

- (instancetype)init{
    
    if (self = [super init]) {
        
        NSString *bundlePath = [[NSBundle mainBundle].resourcePath stringByAppendingPathComponent:@"LBCityPickerView.bundle"];

        NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
        
        NSString *plistPath = [bundle pathForResource:@"kLBCityMsgCachekey" ofType:@"plist"];

        self.dataArray = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
        
        self.cityModelArray = [LBCityMsgModel mj_objectArrayWithKeyValuesArray:self.dataArray];
    }
    return self;
}

+ (NSArray *)getCityArray {
    return [[NSArray alloc]initWithArray:shareCityManager.dataArray copyItems:YES];
}

+ (NSArray <LBCityMsgModel *> *)getCityModelArray {
    return [[NSArray alloc]initWithArray:shareCityManager.cityModelArray copyItems:YES];
}

+ (void)dealWithCityModelByProvinceNo:(NSString *)provinceNo
                             complete:(void(^)(LBCityMsgModel *provinceModel,LBCityMsgModel *cityModel,LBCityMsgModel * areaModel))complete;
{
    NSAssert(provinceNo, @"provinceNo can't be nil");
    
    [shareCityManager.cityModelArray enumerateObjectsUsingBlock:^(LBCityMsgModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj.areaCode isEqualToString:provinceNo]) {
            if (complete) {
                complete(obj,nil,nil);
                *stop = YES;
            }
        }
    }];
}

+ (void)dealWithCityModelByCityNo:(NSString *)cityNo
                         complete:(void(^)(LBCityMsgModel *provinceModel,LBCityMsgModel *cityModel,LBCityMsgModel * areaModel))complete
{
    NSAssert(cityNo, @"cityNo can't be nil");
    
    [shareCityManager.cityModelArray enumerateObjectsUsingBlock:^(LBCityMsgModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj.eachArea enumerateObjectsUsingBlock:^(LBCityMsgModel * _Nonnull childObj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([childObj.areaCode isEqualToString:cityNo]) {
                if (complete) {
                    complete(obj,childObj,nil);
                }
                *stop = YES;
            }
        }];
    }];
}

+ (void)dealWithCityModelByAreaNo:(NSString *)areaNo
                         complete:(void(^)(LBCityMsgModel *provinceModel,LBCityMsgModel *cityModel,LBCityMsgModel * areaModel))complete;
{
    NSAssert(areaNo, @"areaNo can't be nil");
    
    [shareCityManager.cityModelArray enumerateObjectsUsingBlock:^(LBCityMsgModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj.eachArea enumerateObjectsUsingBlock:^(LBCityMsgModel * _Nonnull childObj, NSUInteger idx, BOOL * _Nonnull stop) {
            [childObj.eachArea enumerateObjectsUsingBlock:^(LBCityMsgModel * _Nonnull areaObj, NSUInteger idx, BOOL * _Nonnull stop) {
                if ([areaObj.areaCode isEqualToString:areaNo]) {
                    if (complete) {
                        complete(obj,childObj,areaObj);
                    }
                    *stop = YES;
                }
            }];
        }];
    }];
}

+ (void)dealWithCityModelByByProvinceNo:(NSString *)provinceNo
                                 CityNo:(NSString *)cityNo
                                 AreaNo:(NSString *)areaNo
                               complete:(void(^)(LBCityMsgModel *provinceModel,LBCityMsgModel *cityModel,LBCityMsgModel * areaModel))complete
{
    NSAssert(provinceNo && cityNo && areaNo, @"provinceNo or cityNo or areaNo can't be nil");
    
    [shareCityManager.cityModelArray enumerateObjectsUsingBlock:^(LBCityMsgModel * _Nonnull provinceObj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([provinceObj.areaCode isEqualToString:provinceNo]) {
            [provinceObj.eachArea enumerateObjectsUsingBlock:^(LBCityMsgModel * _Nonnull cityObj, NSUInteger idx, BOOL * _Nonnull stop) {
                if ([cityObj.areaCode isEqualToString:cityNo]) {
                    [cityObj.eachArea enumerateObjectsUsingBlock:^(LBCityMsgModel * _Nonnull areaObj, NSUInteger idx, BOOL * _Nonnull stop) {
                        if (complete) {
                            complete(provinceObj,cityObj,areaObj);
                            *stop = YES;
                        }
                    }];
                }
            }];
        }
    }];
}



@end
